package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DAO.*;
import model.*;

public class QueStudy_AnswerController extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		try {
			processRequest(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String uri = request.getRequestURI();

		String command = uri.substring(uri.lastIndexOf("/") + 1);

		ActionForward forward = null;
		Action action = null;

		if (command != null && command.trim().equals("New.Answer")) {

			HttpSession session = request.getSession();

			int studyId = request.getParameter("studyId") != null
					&& request.getParameter("studyId") != "" ? Integer
					.parseInt(request.getParameter("studyId")) : 0;
			int userId = session.getAttribute("userid") != null
					&& session.getAttribute("userid") != "" ? Integer
					.parseInt((String) session.getAttribute("userid")) : 0;
			String memId = session.getAttribute("memid") != null
					&& session.getAttribute("memid") != "" ? (String) session.getAttribute("memid") : null;
			int questId = request.getParameter("questId") != null
					&& request.getParameter("questId") != "" ? Integer
					.parseInt(request.getParameter("questId")) : 0;

			if (userId < 1 || memId == null) {
				session.invalidate();
				Util.ShowError(response, "로그인해주세요");
			}
			if (studyId < 1 || questId < 1) {
				Util.ShowError(response, "잘못 된 접근입니다");
			}

			AnswerDao answerDao = new AnswerDao();
			ArrayList<Answer> answerList = answerDao.getAllAnswerList(questId);
			System.out.println("answerListsize" + answerList.size());

			for (int i = 0; i < answerList.size(); i++) {

				System.out.println("userId" + userId);
				System.out.println("   " + answerList.get(i).getUid());

				if (!answerDao.getAnswerByUserId(questId, userId).getContent().equals("답변을 작성해주세요.")) {
					Util.ShowError(response, "이미 작성하셨습니다");
				}

			}

			request.setAttribute("studyId", studyId);
			request.setAttribute("questId", questId);

			forward = new ActionForward();
			forward.setPath("/study_quest_makeAnswer.jsp");

		}
		/*
		 * else if (command != null && command.trim().equals("Modify.Answer")){
		 * HttpSession session=request.getSession(); Quest quest = new Quest();
		 * QuestDao questDao = new QuestDao();
		 * 
		 * int studyId = request.getParameter("studyId") != null &&
		 * request.getParameter("studyId") != "" ? Integer
		 * .parseInt(request.getParameter("studyId")) : 0; int userIdCheck =
		 * session.getAttribute("userid") != null &&
		 * session.getAttribute("userid") != "" ? Integer
		 * .parseInt((String)session.getAttribute("userid")) : 0; int questId =
		 * request.getParameter("questId") != null &&
		 * request.getParameter("questId") != "" ? Integer
		 * .parseInt(request.getParameter("questId")) : 0;
		 * 
		 * if (userIdCheck < 1) { session.invalidate(); Util.ShowError(response,
		 * "로그인해주세요"); } if (studyId < 1 || questId < 1) {
		 * Util.ShowError(response, "잘못 된 접근입니다"); }
		 * 
		 * 
		 * 
		 * if(questId>0) { quest=questDao.getQuestInfo(questId);
		 * request.setAttribute("quest", quest); }
		 * 
		 * request.setAttribute("studyId", studyId); forward = new
		 * ActionForward(); forward.setPath("/study_quest_modifyQuest.jsp");
		 * 
		 * }
		 */

		else if (command != null && command.trim().equals("NewAction.Answer")) {
			action = new AnswerNewAction();
			try {

				forward = action.execute(request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}

		} else if (command != null && command.trim().equals("Info.Answer")) {

			action = new AnswerInfoAction();
			try {

				forward = action.execute(request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		else if (command != null
				&& command.trim().equals("ModifyAction.Answer")) {
			action = new QuestModifyAction();
			try {

				forward = action.execute(request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		else if (command != null
				&& command.trim().equals("NewFeedBack.Answer")) {
			action = new AnswerNewFeedBack();
			try {

				forward = action.execute(request, response);

			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		if (forward != null) {
			if (forward.isRedirect()) {
				response.sendRedirect(forward.getPath());

			} else {
				RequestDispatcher dispatcher = request
						.getRequestDispatcher(forward.getPath());

				dispatcher.forward(request, response);
			}
		}
	}
}
